package zhihui.mobile.ui.shop.coupon;

import android.graphics.Color;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.zgm.utils.ui.UiTextHeader;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.sales.CouponInfo;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.coupon.CouponPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/9/6 0006.
 */

public class CouponActivity extends BaseActivity implements ICouponContract.ICouponView
{
    private ICouponContract.ICouponPresenter couponPresenter = new CouponPresenter();

    private UiTextHeader mheader;
    private TextView tv_unused;
    private TextView tv_used;
    private TextView tv_timeout;
    private ListView lv_coupon;

    private CouponAdapter adapter;

    @Override
    public void initView()
    {
        mheader = (UiTextHeader) findViewById(R.id.header_coupon);
        mheader.setTitle("优惠券");
        mheader.setLeftBottonIcon(R.drawable.shop_back);
        mheader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(CouponActivity.class);
            }
        });
        mheader.hindRight();

        tv_unused = (TextView) findViewById(R.id.tv_coupon_activity_unused);
        tv_unused.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                changeViewState(v);
            }
        });

        tv_used = (TextView) findViewById(R.id.tv_coupon_activity_used);
        tv_used.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                changeViewState(v);
            }
        });

        tv_timeout = (TextView) findViewById(R.id.tv_coupon_activity_timeout);
        tv_timeout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                changeViewState(v);
            }
        });

        lv_coupon = (ListView) findViewById(R.id.lv_coupon_activity_content);
        adapter = new CouponAdapter(this);
        lv_coupon.setAdapter(adapter);

        couponPresenter.getCouponListByState(1);
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.coupon_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{couponPresenter};
    }

    private void changeViewState(View view)
    {
        tv_unused.setTextColor(Color.parseColor("#333333"));
        tv_used.setTextColor(Color.parseColor("#333333"));
        tv_timeout.setTextColor(Color.parseColor("#333333"));

        if (tv_unused == view)
        {
            tv_unused.setTextColor(Color.parseColor("#ff6537"));
            couponPresenter.getCouponListByState(1);
        } else if (tv_used == view)
        {
            tv_used.setTextColor(Color.parseColor("#ff6537"));
            couponPresenter.getCouponListByState(2);
        } else if (tv_timeout == view)
        {
            tv_timeout.setTextColor(Color.parseColor("#ff6537"));
            couponPresenter.getCouponListByState(3);
        }
    }

    @Override
    protected void onInitPresenter()
    {
        couponPresenter.init(CouponActivity.this, CouponActivity.this);
    }

    @Override
    public void refreshCoupons(List<CouponInfo> list, int unusedNum, int useNum, int timeoutNum)
    {
        adapter.setList(list);
        adapter.notifyDataSetChanged();
        tv_unused.setText("未使用(" + unusedNum + ")");
        tv_used.setText("已使用(" + useNum + ")");
        tv_timeout.setText("已过期(" + timeoutNum + ")");
    }
}
